package hn.cch.advanced.thread;

import java.util.concurrent.TimeUnit;

/**
 * 守护线程
 */
public class DaemonThread {

    public static void main(String[] args) {

        daemon();
    }

    public static void daemon() {

        long startTime = System.nanoTime();

        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    TimeUnit.SECONDS.sleep(Long.MAX_VALUE);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "daemon_thread");

        // thread.setDaemon(true);//守护线程
        //当所有非守护线程退出后程序才会退出

        thread.start();
        try {
            TimeUnit.SECONDS.sleep(10L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long runTime = (System.nanoTime() - startTime) / 1000 / 1000 / 1000;
        System.out.println("run time = " + runTime + " s");

    }
}
